package drds.binlog.common.zookeeper.running;

import drds.binlog.common.AbstractLifeCycle;
import drds.binlog.common.utils.BooleanMutex;
import drds.binlog.common.utils.JsonUtils;
import drds.binlog.common.zookeeper.ZooKeeperClient;
import drds.binlog.common.zookeeper.Zookeepers;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.exception.ZkException;
import org.I0Itec.zkclient.exception.ZkInterruptedException;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.I0Itec.zkclient.exception.ZkNodeExistsException;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 针对server的running节点控制
 */
public class Monitor extends AbstractLifeCycle
{

    private static final Logger logger = LoggerFactory.getLogger(Monitor.class);
    private ZooKeeperClient zooKeeperClient;
    private String destination;
    private IZkDataListener dataListener;
    private BooleanMutex mutex = new BooleanMutex(false);
    private volatile boolean release = false;
    // 当前服务节点状态信息
    private ServerRunningData serverRunningData;
    // 当前实际运行的节点状态信息
    private volatile ServerRunningData activeData;
    private ScheduledExecutorService delayExector = Executors.newScheduledThreadPool(1);
    private int delayTime = 5;
    private ServerRunningListener listener;

    public Monitor(ServerRunningData serverRunningData)
    {
        this();
        this.serverRunningData = serverRunningData;
    }

    public Monitor()
    {
        // 创建父节点
        dataListener = new IZkDataListener()
        {

            public void handleDataChange(String dataPath, Object data) throws Exception
            {
                MDC.put("destination", destination);
                ServerRunningData serverRunningData = JsonUtils.unmarshalFromByte((byte[]) data, ServerRunningData.class);
                if (!isMine(serverRunningData.getAddress()))
                {
                    mutex.set(false);
                }

                if (!serverRunningData.isActive() && isMine(serverRunningData.getAddress()))
                { // 说明出现了主动释放的操作，并且本机之前是active
                    release = true;
                    releaseRunning();// 彻底释放mainstem
                }

                activeData = serverRunningData;
            }

            public void handleDataDeleted(String dataPath) throws Exception
            {
                MDC.put("destination", destination);
                mutex.set(false);
                if (!release && activeData != null && isMine(activeData.getAddress()))
                {
                    // 如果上一次active的状态就是本机，则即时触发一下active抢占
                    initRunning();
                } else
                {
                    // 否则就是等待delayTime，避免因网络瞬端或者zk异常，导致出现频繁的切换操作
                    delayExector.schedule(new Runnable()
                    {

                        public void run()
                        {
                            initRunning();
                        }
                    }, delayTime, TimeUnit.SECONDS);
                }
            }

        };

    }

    public void init()
    {
        processStart();
    }

    public synchronized void start()
    {
        super.start();
        try
        {
            processStart();
            if (zooKeeperClient != null)
            {
                // 如果需要尽可能释放instance资源，不需要监听running节点，不然即使stop了这台机器，另一台机器立马会start
                String path = Zookeepers.getDestinationServerRunning(destination);
                zooKeeperClient.subscribeDataChanges(path, dataListener);

                initRunning();
            } else
            {
                processActiveEnter();// 没有zk，直接启动
            }
        } catch (Exception e)
        {
            logger.error("start failed", e);
            // 没有正常启动，重置一下状态，避免干扰下一次start
            stop();
        }

    }

    public void release()
    {
        if (zooKeeperClient != null)
        {
            releaseRunning(); // 尝试一下release
        } else
        {
            processActiveExit(); // 没有zk，直接启动
        }
    }

    public synchronized void stop()
    {
        super.stop();

        if (zooKeeperClient != null)
        {
            String path = Zookeepers.getDestinationServerRunning(destination);
            zooKeeperClient.unsubscribeDataChanges(path, dataListener);

            releaseRunning(); // 尝试一下release
        } else
        {
            processActiveExit(); // 没有zk，直接启动
        }
        processStop();
    }

    private void initRunning()
    {
        if (!isRunning())
        {
            return;
        }

        String path = Zookeepers.getDestinationServerRunning(destination);
        // 序列化
        byte[] bytes = JsonUtils.marshalToByte(serverRunningData);
        try
        {
            mutex.set(false);
            zooKeeperClient.create(path, bytes, CreateMode.EPHEMERAL);
            activeData = serverRunningData;
            processActiveEnter();// 触发一下事件
            mutex.set(true);
        } catch (ZkNodeExistsException e)
        {
            bytes = zooKeeperClient.readData(path, true);
            if (bytes == null)
            {// 如果不存在节点，立即尝试一次
                initRunning();
            } else
            {
                activeData = JsonUtils.unmarshalFromByte(bytes, ServerRunningData.class);
            }
        } catch (ZkNoNodeException e)
        {
            zooKeeperClient.createPersistent(Zookeepers.getDestinationPath(destination), true); // 尝试创建父节点
            initRunning();
        }
    }

    /**
     * 阻塞等待自己成为active，如果自己成为active，立马返回
     *
     * @throws InterruptedException
     */
    public void waitForActive() throws InterruptedException
    {
        initRunning();
        mutex.get();
    }

    /**
     * 检查当前的状态
     */
    public boolean check()
    {
        String path = Zookeepers.getDestinationServerRunning(destination);
        try
        {
            byte[] bytes = zooKeeperClient.readData(path);
            ServerRunningData eventData = JsonUtils.unmarshalFromByte(bytes, ServerRunningData.class);
            activeData = eventData;// 更新下为最新值
            // 检查下nid是否为自己
            boolean result = isMine(activeData.getAddress());
            if (!result)
            {
                logger.warn("canal is running in node[{}] , but not in node[{}]",
                        activeData.getCid(),
                        serverRunningData.getCid());
            }
            return result;
        } catch (ZkNoNodeException e)
        {
            logger.warn("canal is not run any in node");
            return false;
        } catch (ZkInterruptedException e)
        {
            logger.warn("canal check is interrupt");
            Thread.interrupted();// 清除interrupt标记
            return check();
        } catch (ZkException e)
        {
            logger.warn("canal check is failed");
            return false;
        }
    }

    private boolean releaseRunning()
    {
        if (check())
        {
            String path = Zookeepers.getDestinationServerRunning(destination);
            zooKeeperClient.delete(path);
            mutex.set(false);
            processActiveExit();
            return true;
        }

        return false;
    }

    // ====================== helper method ======================

    private boolean isMine(String address)
    {
        return address.equals(serverRunningData.getAddress());
    }

    private void processStart()
    {
        if (listener != null)
        {
            try
            {
                listener.processStart();
            } catch (Exception e)
            {
                logger.error("processStart failed", e);
            }
        }
    }

    private void processStop()
    {
        if (listener != null)
        {
            try
            {
                listener.processStop();
            } catch (Exception e)
            {
                logger.error("processStop failed", e);
            }
        }
    }

    private void processActiveEnter()
    {
        if (listener != null)
        {
            listener.processActiveEnter();
        }
    }

    private void processActiveExit()
    {
        if (listener != null)
        {
            try
            {
                listener.processActiveExit();
            } catch (Exception e)
            {
                logger.error("processActiveExit failed", e);
            }
        }
    }

    public void setListener(ServerRunningListener listener)
    {
        this.listener = listener;
    }

    // ===================== setter / getter =======================

    public void setDelayTime(int delayTime)
    {
        this.delayTime = delayTime;
    }

    public void setServerRunningData(ServerRunningData serverRunningData)
    {
        this.serverRunningData = serverRunningData;
    }

    public void setDestination(String destination)
    {
        this.destination = destination;
    }

    public void setZooKeeperClient(ZooKeeperClient zooKeeperClient)
    {
        this.zooKeeperClient = zooKeeperClient;
    }

}
